package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.TeacherViewer;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.StringUtils;

public class ChatRowSubject extends BaseChatRow{

//    private TextView contentView;

    /**
     type                                    记录类型   1:图片 2:视频 3:音频 4:文字 5:套餐 6:评价  7:公开课

     url                                     素材地址
     content                                 文字内容
     cover                                   视频封面
     timelen                                 音频时长
     headimgurl                              头像

     extendfield1                            扩展字段1   type=5时表示套餐id

     type=6时表示评价结果   1:满意  2:一般  3:不满意

     type=7时表示公开课课题id


     extendfield2                            扩展字段2   type=5时表示套餐标题     type=7时表示课题名称

     extendfield3                            扩展字段3   type=5时表示套餐价格     type=7时表示课题价格

     extendfield4                            扩展字段4   type=5时表示套餐折扣价   type=7时表示 开课时间 + 结课时间

     extendfield5                            扩展字段5   type=5时表示套餐售出量   type=7时表示课题购买量(即课题报名数)

     extendfield6                            扩展字段6   type=5时表示套餐库存

     userlist                                课题下的老师列表    type=7时使用
     */

    private TextView mSubjectTitle;
    private TextView mSubjectTime;
    private TeacherViewer mTeacherViewer;
    private TextView mSubjectPrice;
    private TextView mSubjectCount;

    private TextView mNickName;
    private boolean mShowNickName;


    public ChatRowSubject(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public ChatRowSubject(Context context, ChatMessageEntity message, int position, boolean showNickName, BaseAdapter adapter) {
        super(context, message, position, adapter);
        mShowNickName = showNickName;
    }


    @Override
    protected void onInflateView() {
        inflater.inflate(R.layout.ease_row_received_subject , this);
    }

    @Override
    protected void onFindViewById() {
//        contentView = (TextView) findViewById(R.id.tv_chatcontent);

        mSubjectTitle = (TextView) findViewById(R.id.subject_title);
        mSubjectTime = (TextView) findViewById(R.id.subject_time);
        mTeacherViewer = (TeacherViewer) findViewById(R.id.teacher_list);
        mSubjectPrice = (TextView) findViewById(R.id.subject_price);
        mSubjectCount = (TextView) findViewById(R.id.subject_count);
        mNickName = (TextView) findViewById(R.id.tv_userid);
    }

    @Override
    public void onSetUpView() {
//        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
//        Spannable span = EaseSmileUtils.getSmiledText(context, message.getContent());//txtBody.getMessage());
        // 设置内容
//        contentView.setText(span, TextView.BufferType.SPANNABLE);

        mSubjectTitle.setText(message.getExtendfield2());
        if(TextUtils.isEmpty(message.getExtendfield4())){
            mSubjectTime.setVisibility(View.GONE);
        }else{
            mSubjectTime.setVisibility(View.VISIBLE);
            mSubjectTime.setText(message.getExtendfield4());
        }
        mTeacherViewer.initData(message.getUserlist());
        mSubjectPrice.setText(message.getExtendfield3());
        mSubjectCount.setText(message.getExtendfield5() + "人已报名");

        if(mShowNickName) {
            if(message.direct() == EMMessage.Direct.RECEIVE){
                mNickName.setText(StringUtils.getSpltString(message.getUsername(), 10));
            }else{
                mNickName.setText("[" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getStudentname(), 10) + "]" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getUsername(), 10));
            }
        }

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL:
//                    progressBar.setVisibility(View.GONE);
//                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS:
//                    progressBar.setVisibility(View.VISIBLE);
//                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();

//        if(TextUtils.isEmpty(message.getExtendfield4())){
//            mSubjectTime.setVisibility(View.GONE);
//        }else{
//            mSubjectTime.setVisibility(View.VISIBLE);
//            mSubjectTime.setText(message.getExtendfield4());
//        }

    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
//        ProductDetailActivity.startActivity(getContext(), message.getExtendfield1());
        ProductDetailActivity.startActivity(getContext(), message.getExtendfield1(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
    }
}
